package com.hymjweb.demo.hymj.system_maintenance.permission.controller;

import com.alibaba.fastjson.JSONArray;
import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.hymj.system_maintenance.permission.bean.PermissionPojo;
import com.hymjweb.demo.hymj.system_maintenance.permission.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/pages/system_maintain/permission")
public class PermissionController extends BaseController {

    @Autowired
    private PermissionService permissionService;

    @RequestMapping("/permissionInit")
    public String permissionInit() {
        return "pages/system_maintain/permission/permission";
    }

    @RequestMapping("/query")
    @ResponseBody
    public Object query(@RequestParam(required = false) Map param) {
        return permissionService.getPageList(param);
    }

    @RequestMapping("/queryNoPage")
    @ResponseBody
    public Object queryNoPage(@RequestParam Map param) throws Exception {
        return permissionService.getPermissionPojoListNoPage(param);
    }


    @RequestMapping("/loadMenuTree")
    @ResponseBody
    public Object loadMenuTree() throws Exception {
        return permissionService.loadMenuTree();
    }


    @RequestMapping("/refreshAuthMenuResource")
    @ResponseBody
    public Object refreshAuthMenuResource() {
        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("授权菜单资源刷新成功!");

        int affectRows = permissionService.refreshAuthMenuResource();

        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("授权菜单资源刷新失败!");
        }

        return baseResultMap;
    }

    @RequestMapping("/save")
    @ResponseBody
    public Object save(@RequestBody(required = true) JSONArray jsonArray) throws Exception {
        if (jsonArray == null || jsonArray.isEmpty()) {
            throwException("保存授权资源参数错误!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();

        List<PermissionPojo> permissionPojoList = permissionService.jsonArrayConvertToPojoList(jsonArray);

        if (permissionPojoList == null || permissionPojoList.isEmpty()) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("没有需要保存的授权资源!");
            return baseResultMap;
        }

        // 校验是否有相同的 menuId,
        boolean flag = permissionService.checkMenuId(permissionPojoList);

        if (!flag) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("授权资源列表中有不相同的模块名称，不能做保存操作!");
            return baseResultMap;
        }

        // 1.先根据模块 ID 删除已经存的模块数据
        String menuId = permissionPojoList.get(0).getMenuId();

        int deletePermissionByMenuId = permissionService.deletePermissioByMenuId(menuId);

        // 2.检验是否有相同的资源 PermissionCod
        boolean uniqueFlag = permissionService.checkUnique(permissionPojoList);
        if (uniqueFlag) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("授权资源列表中有相同的权限编码，保存授权资源失败!");
            return baseResultMap;
        }

        // 3.可以批量添加资源了

        int affectRows = 0;
        for (PermissionPojo pojo : permissionPojoList) {
            int affecrRow = permissionService.add(pojo);
            affectRows += affecrRow;
        }

        baseResultMap.setOk(true);
        baseResultMap.setMessage("保存授权资源成功!");

        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("保存授权资源失败!");
        }

        return baseResultMap;

    }


}
